package com.ruoyi.security.convert;

import com.ruoyi.security.domain.SecurityDayMinuteTrade;
import com.ruoyi.security.response.SecurityDayMinuteTradeResponse;
import com.ruoyi.security.util.DateUtil;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static org.mapstruct.factory.Mappers.getMapper;

/**
 * @author yeoman
 * @date 2025/5/4 11:19
 * @description
 */
@Mapper(imports = {BigDecimal.class})
public interface SecurityDayMinuteTradeConvert {

    SecurityDayMinuteTradeConvert INSTANCE = getMapper(SecurityDayMinuteTradeConvert.class);

    /**
     * 解析日期
     * @param time 时间字符串 2024/11/28/四11:30
     * @return 日期对象
     */
    @Named("parseDate")
    default Date parseDate(String time) {
        String date = time.substring(0, 4) + "-" + time.substring(5, 7) + "-" + time.substring(8, 10);
        return DateUtil.parseDate(date);
    }
     /**
      * 解析成交量
      * @param volume 成交量字符串 123456手 123456万手 123456千万手
      * @return 成交量
      */
    @Named("parseVolume")
    default BigDecimal parseVolume(String volume) {
        // 移除所有空格
        volume = volume.replaceAll("\\s+", "");

        // 如果包含"千万手"，转换为手
        if (volume.contains("千万手")) {
            String numberStr = volume.replace("千万手", "");
            // 千万手转手：乘以10,000,000
            BigDecimal number = new BigDecimal(numberStr);
            return number.multiply(BigDecimal.valueOf(10000000));
        }
        // 如果包含"万手"，转换为手
        else if (volume.contains("万手")) {
            String numberStr = volume.replace("万手", "");
            BigDecimal number = new BigDecimal(numberStr);
            return number.multiply(BigDecimal.valueOf(10000));
        }
        // 如果包含"手"，直接返回数字部分
        else if (volume.contains("手")) {
            volume = volume.replace("手", "");
            return new BigDecimal(volume);
        }
        // 如果只是数字，直接返回
        return new BigDecimal(volume);
    }

    /**
     * 解析涨跌幅范围
     * @param riseFall 涨跌幅字符串 4.21(0.36%)
     * @return 涨跌幅 14.21
     */
    @Named("parseRiseFallRange")
    default BigDecimal parseRiseFallRange(String riseFall) {
        String[] riseFallArr = riseFall.split("\\(");
        return new BigDecimal(riseFallArr[0]);
    }

    /**
     * 解析涨跌幅百分比
     * @param riseFall 涨跌幅字符串 4.21(0.36%)
     * @return 涨跌幅百分比 0.36
     */
    @Named("parseRiseFallPercentage")
    default BigDecimal parseRiseFallPercentage(String riseFall) {
        String[] riseFallArr = riseFall.split("\\(");
        String percentageStr = riseFallArr[1].replace("%)", "");
        return new BigDecimal(percentageStr);
    }

    List<SecurityDayMinuteTrade> toSecurityDayMinuteTradeList(List<SecurityDayMinuteTradeResponse> list);

    @Mappings({
            @Mapping(source = "time", target = "date", qualifiedByName = "parseDate"),
            @Mapping(target = "time", expression = "java(response.getTime().substring(12, 17))"),
            @Mapping(target = "weekday", expression = "java(response.getTime().substring(11, 12))"),
            @Mapping(source = "volume", target = "volume", qualifiedByName = "parseVolume"),
            @Mapping(target = "averagePrice", expression = "java(new BigDecimal(response.getAveragePrice()))"),
            @Mapping(target = "price", expression = "java(new BigDecimal(response.getPrice()))"),
            @Mapping(source = "riseFall", target = "riseFallRange", qualifiedByName = "parseRiseFallRange"),
            @Mapping(source = "riseFall", target = "riseFallPercentage", qualifiedByName = "parseRiseFallPercentage")
    })
    SecurityDayMinuteTrade toSecurityDayMinuteTrade(SecurityDayMinuteTradeResponse response);

}
